<?php

include "function_xml_basic.php";

$blacklist = "/MINI/SECURITY/VIRUS/HTTP/BLACKLISTS/BLACKLIST";
$whitelist = "/MINI/SECURITY/VIRUS/HTTP/WHITELISTS/WHITELIST";

function GetXmlBlackList()
{
	global $blacklist;
	$query_string = $blacklist;
	$key_array = array("Content", "Type", "ListType");
	return GetAttributList($query_string, $key_array);
}
function GetXmlWhiteList()
{
	global $whitelist;
	$query_string = $whitelist;
	$key_array = array("Content", "Type", "ListType");
	return GetAttributList($query_string, $key_array);
}
function DelXmlSpecialBlackList($s)
{
	global $blacklist;

	$query_string = $blacklist. "[@Content='". $s. "']";
	DelSpecialNode($query_string);
	ApplyToHavpBWList();
}

function DelXmlSpecialWhiteList($s)
{
	global $whitelist;

	$query_string = $whitelist. "[@Content='". $s. "']";
	DelSpecialNode($query_string);
	ApplyToHavpBWList();
}

function AppendXmlBlackList($in_array)
{
	$blacklist = "/MINI/SECURITY/VIRUS/HTTP/BLACKLISTS";
        $node_name = "BLACKLIST";
	$query_string = $blacklist;
	$in_array['Type'] = "1";
	AppendAllAttrOfNode($query_string, $node_name, $in_array);
	ApplyToHavpBWList();
}
function AppendXmlWhiteList($in_array)
{
	$whitelist = "/MINI/SECURITY/VIRUS/HTTP/WHITELISTS";
        $node_name = "WHITELIST";
	$query_string = $whitelist;
	$in_array['Type'] = "1";
	AppendAllAttrOfNode($query_string, $node_name, $in_array);
	ApplyToHavpBWList();
}

//$list = GetXmlBlackList();
//foreach ($list as $l)
//{
//	print $l["Content"]. "\n". $l["Type"]. "\n". $l["ListType"]. "\n";
//}

function ApplyToHavpBWList()
{
	$blacklist = "/usr/local/etc/havp/blacklist";
	$whitelist = "/usr/local/etc/havp/whitelist";
	$echo_cmd = "/bin/echo ";

	$command = "";
	$command .= $echo_cmd. " > ". $blacklist. " ;";
	$command .= $echo_cmd. " > ". $whitelist. " ;";

	$list = GetXmlBlackList();
	foreach ($list as $l )
	{
		if ($l["ListType"] == 1) // file 
		{
			$command .= $echo_cmd. ' "*/*.'. $l["Content"]. '" >> '. $blacklist. " ;" ;
		}
		if ($l["ListType"] == 2) // url
		{
			$command .= $echo_cmd. ' "'. $l["Content"]. '/*" >> '. $blacklist. " ;";
		}
	}
	$list = GetXmlWhiteList();
	foreach ($list as $l )
	{
		if ($l["ListType"] == 1) // file 
		{
			$command .= $echo_cmd. ' "*/*.'. $l["Content"]. '" >> '. $whitelist. " ;" ;
		}
		if ($l["ListType"] == 2) // url
		{
			$command .= $echo_cmd. ' "'. $l["Content"]. '/*" >> '. $whitelist. " ;";
		}
	}
	$command_stop = '/bin/kill -TERM `/bin/cat /var/run/havp.pid `;';
	$command_start = '/usr/local/sbin/havp -c /usr/local/etc/havp/havp.config ;';
	$command .= $command_stop. $command_start;
	//print $command;
	$ret = shell_exec($command);
	return $ret;
}

?>
